<template>
  <div class="dashboard-container">
    <div class="app-container">
      <el-card v-loading="loading">
        <el-tabs v-model="activeName">
          <el-tab-pane label="角色管理" name="role">
            <!-- 新增按钮 -->
            <el-button
              type="primary"
              icon="el-icon-plus"
              size="small"
              style="margin-top:20px"
              @click="showDialog = true"
            >新增角色</el-button>
            <el-table :data="tableData">
              <el-table-column label="序号" type="index" align="center" :index="indexMethod" />
              <el-table-column label="角色名称" prop="name" align="center" />
              <el-table-column label="描述" prop="description" align="center" />
              <el-table-column label="操作" align="center">
                <template v-slot="{row}">
                  <el-button size="small" type="success">分配权限</el-button>
                  <el-button size="small" type="primary" @click="edit(row.id)">编辑</el-button>
                  <el-button size="small" type="danger" @click="del(row.id)">删除</el-button>
                </template>
              </el-table-column>
            </el-table>
            <el-pagination
              :current-page="page"
              :page-sizes="[6, 12, 18, 24]"
              :page-size="pagesize"
              background
              layout="total, sizes, prev, pager, next, jumper"
              :total="total"
              @size-change="handleSizeChange"
              @current-change="handleCurrentChange"
            />
          </el-tab-pane>
          <el-tab-pane label="公司信息" name="company">
            <!-- 警告信息 -->
            <el-alert
              title="对公司名称、公司地址、营业执照、公司地区的更新，将使得公司资料被重新审核，请谨慎修改"
              type="info"
              show-icon
              :closable="false"
            />
            <!-- 表单 -->
            <el-form ref="company" label-width="120px" style="margin-top:50px" :model="company" :rules="rules">
              <el-form-item label="公司名称">
                <el-input v-model="company.name" disabled style="width:400px" />
              </el-form-item>
              <el-form-item label="公司地址">
                <el-input v-model="company.companyAddress" disabled style="width:400px" />
              </el-form-item>
              <el-form-item label="邮箱">
                <el-input v-model="company.mailbox" disabled style="width:400px" />
              </el-form-item>
              <el-form-item label="备注">
                <el-input v-model="company.remarks" type="textarea" :rows="3" disabled style="width:400px" />
              </el-form-item>
            </el-form>
          </el-tab-pane>
          <!-- 这是对话框 -->
          <el-dialog
            :title="form.id ? '编辑角色':'添加角色'"
            :visible="showDialog"
            width="40%"
            @close="close"
          >
            <el-form ref="form" :model="form" :rules="rules" label-width="100px">
              <el-form-item label="角色名称" prop="name">
                <el-input v-model="form.name" placeholder="请输入角色名称" />
              </el-form-item>
              <el-form-item label="角色描述" prop="description">
                <el-input v-model="form.description" placeholder="请输入角色描述" />
              </el-form-item>
            </el-form>
            <template #footer>
              <el-button @click="close">取消</el-button>
              <el-button type="primary" @click="add">确定</el-button>
            </template>
          </el-dialog>
        </el-tabs>
      </el-card>
    </div>
  </div>
</template>

<script>
import { getAllRoles, delRole, addRole, getRoleDetailById, updateRole, getCompanyById } from '@/api/settings'
export default {
  name: 'Settings',
  data() {
    // 0注意这里里是发送 ajax 请求获取最新的数据
    const validateName = async(rule, value, callback) => {
      const res = await getAllRoles(1, this.total)
      res.rows.some(item => item.name === value) ? callback(new Error('角色名重复')) : callback()
    }
    return {
      activeName: 'role',
      tableData: [], // 表格绑定的数据
      page: 1,
      pagesize: 6,
      total: 0,
      showDialog: false,
      form: {
        name: '',
        description: ''
      },
      rules: {
        name: [
          { required: true, message: '角色名称不能为空' },
          { validator: validateName, trigger: 'blur' }
        ]
      },
      company: {
        name: '',
        companyAddress: '',
        mailbox: '',
        remarks: ''
      },
      loading: false
    }
  },
  watch: {
    async activeName(e) {
      if (e === 'company') {
        const id = this.$store.state.user.userInfo.companyId
        // console.log(id)
        const res = await getCompanyById(id)
        this.company = res
      }
    }
  },
  created() {
    this.getAllRoles()
  },
  methods: {
    // 1. 获取数据渲染
    async getAllRoles() {
      this.loading = true
      const res = await getAllRoles(this.page, this.pagesize)
      // console.log(res)
      this.tableData = res.rows
      this.total = res.total
      this.loading = false
    },
    // 2. 跳转到的当前页码
    async handleCurrentChange(val) {
      this.page = val
      const res = await getAllRoles(this.page, this.pagesize)
      this.tableData = res.rows
    },
    // 3. 每页显示的数据
    async handleSizeChange(val) {
      this.page = 1
      this.pagesize = val
      const res = await getAllRoles(this.page, this.pagesize)
      this.tableData = res.rows
    },
    // 4.删除功能
    async del(id) {
      try {
        await this.$confirm('你确定删除此角色吗?', '温馨提示', {
          type: 'warning'
        })
      } catch {
        return
      }
      await delRole(id)
      this.$message.success('删除角色成功')

      //
      if (this.tableData.length === 1 && this.pagesize > 1) {
        this.page--
      }
      // 重新渲染
      this.getAllRoles()
    },
    // 5. 添加角色____________________________________________
    async add() {
      try {
        await this.$refs.form.validate()
      } catch {
        return
      }
      if (this.form.id) {
        await updateRole(this.form)
        this.$message.success('更新角色成功')
      } else {
        await addRole(this.form)
        this.$message.success('添加角色成功')
        this.total++
        this.page = Math.ceil(this.total / this.pagesize)
        // this.getAllRoles()
      }
      // 重新渲染
      this.getAllRoles()
      this.showDialog = false
    },
    // 6.重置表单
    close() {
      this.$refs.form.resetFields()
      this.form = {
        name: '',
        description: ''
      }
      this.showDialog = false
    },
    // 7.编辑角色
    async edit(id) {
      const res = await getRoleDetailById(id)
      // console.log(res)
      this.form = res
      this.showDialog = true
    },
    // 8.处理序号
    indexMethod(index) {
      return index + 1 + (this.page - 1) * this.pagesize
    }
  }
}
</script>

<style>

</style>
